package com.caffeinatedbliss.nanowrimo.servlet;

import com.caffeinatedbliss.nanowrimo.NanoWebserviceUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: Paul Hawke
 * Date: Oct 6, 2007
 * Time: 7:26:10 AM
 * To change this template use File | Settings | File Templates.
 */
public class UrlProcessor {
  public static final String[] FORMATS = {
    /* -ppl -avg -pc -dy -wc */ "{0,number,#}",
    /* -ppl -avg -pc -dy +wc */ "{0,number,#} words",
    /* -ppl -avg -pc +dy -wc */ "{1,number,#} days left",
    /* -ppl -avg -pc +dy +wc */ "{0,number,#}, {1,number,#} days left",
    /* -ppl -avg +pc -dy -wc */ "{2,number,#.#}% complete",
    /* -ppl -avg +pc -dy +wc */ "{0,number,#} words ({2,number,#.#}%)",
    /* -ppl -avg +pc +dy -wc */ "{2,number,#.#}%, {1,number,#} days",
    /* -ppl -avg +pc +dy +wc */ "{0,number,#} ({2,number,#.#}%), {1,number,#} days",
    /* -ppl +avg -pc -dy -wc */ "{3,number,#} / person",
    /* -ppl +avg -pc -dy +wc */ "{0,number,#} words ({3,number,#}/person)",
    /* -ppl +avg -pc +dy -wc */ "{1,number,#} days, {3,number,#}/person",
    /* -ppl +avg -pc +dy +wc */ "{0,number,#} ({3,number,#}/person), {1,number,#} days",
    /* -ppl +avg +pc -dy -wc */ "{2,number,#.#}% complete, ({3,number,#}/person)",
    /* -ppl +avg +pc -dy +wc */ "{0,number,#} ({3,number,#}/person) ({2,number,#.#}%)",
    /* -ppl +avg +pc +dy -wc */ "{2,number,#.#}%, {1,number,#} days ({3,number,#}/person)",
    /* -ppl +avg +pc +dy +wc */ "{0,number,#} ({2,number,#.#}%, {3,number,#}/person), {1,number,#} days",
    /* +ppl -avg -pc -dy -wc */ "{0,number,#}, {4,number,#} people",
    /* +ppl -avg -pc -dy +wc */ "{0,number,#} words, {4,number,#} people",
    /* +ppl -avg -pc +dy -wc */ "{1,number,#} days left, {4,number,#} people",
    /* +ppl -avg -pc +dy +wc */ "{0,number,#}, {1,number,#} days, {4,number,#} people",
    /* +ppl -avg +pc -dy -wc */ "{2,number,#.#}% complete, {4,number,#} people",
    /* +ppl -avg +pc -dy +wc */ "{0,number,#} words ({2,number,#.#}%) {4,number,#} people",
    /* +ppl -avg +pc +dy -wc */ "{2,number,#.#}%, {1,number,#} days, {4,number,#} people",
    /* +ppl -avg +pc +dy +wc */ "{0,number,#} ({2,number,#.#}%), {1,number,#} days, {4,number,#} people",
    /* +ppl +avg -pc -dy -wc */ "{0,number,#} ({3,number,#}/person, {4,number,#} people)",
    /* +ppl +avg -pc -dy +wc */ "{0,number,#} ({3,number,#}/person, {4,number,#} people)",
    /* +ppl +avg -pc +dy -wc */ "{1,number,#} days left, {4,number,#} people",
    /* +ppl +avg -pc +dy +wc */ "{0,number,#}, {1,number,#} days, {4,number,#} people",
    /* +ppl +avg +pc -dy -wc */ "{2,number,#.#}% complete, {4,number,#} people",
    /* +ppl +avg +pc -dy +wc */ "{0,number,#} words ({2,number,#.#}%) {4,number,#} people",
    /* +ppl +avg +pc +dy -wc */ "{2,number,#.#}%, {1,number,#} days, {4,number,#} people",
    /* +ppl +avg +pc +dy +wc */ "{0,number,#} ({2,number,#.#}%), {1,number,#} days, {4,number,#} people",
  };
  private Integer goal;
  private ArrayList<String> idList;
  private int showWc = 1;
  private int showDays = 0;
  private int showPercent = 0;
  private int showPeople = 0;
  private int showAverage = 0;

  public UrlProcessor(HttpServletRequest request) {
    readGoal(request);
    parseFilename(request);
  }

  private void parseFilename(HttpServletRequest request) {
    StringTokenizer tok = new StringTokenizer(NanoWebserviceUtils.getFilename(request), "-.");
    idList = new ArrayList<String>();
    while (tok.hasMoreTokens()) {
      String next = tok.nextToken().toLowerCase();

      if (next.startsWith("wc")) {
        showWc = 0;
      } else if (next.startsWith("days")) {
        showDays = 1;
      } else if (next.startsWith("pc")) {
        showPercent = 1;
      } else if (next.startsWith("people")) {
        showPeople = 1;
      } else if (next.startsWith("average")) {
        showAverage = 1;
      } else if (next.startsWith("goal=")) {
        goal = new Integer(next.substring(next.indexOf('=') + 1));
      } else {
        try {
          Integer.parseInt(next);
          idList.add(next);
        } catch (NumberFormatException e) {
        }
      }
    }
  }

  private void readGoal(HttpServletRequest request) {
    String goalStr = request.getParameter("goal");
    if (goalStr != null) {
      try {
        goal = new Integer(goalStr);
      } catch (NumberFormatException e) {}
    }
  }

  public boolean isGoalInUrl() {
    return goal != null;
  }

  public int getGoal() {
    return goal != null ? goal.intValue() : 50000;
  }

  public String getFirstId() {
    return (idList.size() > 0) ? idList.get(0) : "";
  }

  public String getWordcountFormat() {
    int index = showPeople * 16 + showAverage * 8 + showPercent * 4 + showDays * 2 + showWc;
    return FORMATS[index];
  }

  public List getIdList() {
    return idList;
  }

  public void resetParameters(int wc, int days, int pc) {
    showWc = wc;
    showDays = days;
    showPercent = pc;
  }

  public void resetAggregate(int people, int average) {
    showAverage = average;
    showPeople = people;
  }
}
